import 'package:flutter/material.dart';

void main() => runApp(RootApp());

// 定义根目录Wigdget
class RootApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 根视图
    return MaterialApp(
      // theme: ThemeData(primaryColor: Colors.white),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectIndex = 0;
  List<HomeItemPage> homeItemList = [
    HomeItemPage(1),
    HomeItemPage(2),
    HomeItemPage(3),
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("首页页面切换")),
      body: homeItemList[_selectIndex],
      bottomNavigationBar: buildBottom(),
    );
  }

  buildBottom() {
    return BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
          BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),
          BottomNavigationBarItem(icon: Icon(Icons.people), label: "我的"),
        ],
        currentIndex: _selectIndex,
        onTap: (int index) {
          setState(() {
            _selectIndex = index;
          });
        });
  }
}

// ignore: must_be_immutable
class HomeItemPage extends StatelessWidget {
  int index = 0;
  HomeItemPage(this.index);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: selcetColor(),
      body: Container(
        child: Text("页面 $index"),
      ),
    );
  }

  selcetColor() {
    if (index == 1) {
      return Colors.grey;
    } else if (index == 2) {
      return Colors.blue;
    } else {
      return Colors.green;
    }
  }
}
